const fs = require('fs');
const path = require('path');

// 检查哪些依赖在代码中未被使用
const packageJson = require('../package.json');
const nodeModulesPath = path.join(__dirname, '..', 'node_modules');

// 读取所有源代码文件
function readAllSourceFiles(dir) {
  let results = [];
  const list = fs.readdirSync(dir);
  list.forEach(file => {
    file = path.join(dir, file);
    const stat = fs.statSync(file);
    if (stat && stat.isDirectory()) {
      results = [...results, ...readAllSourceFiles(file)];
    } else {
      if (file.endsWith('.ts') || file.endsWith('.tsx') || file.endsWith('.js') || file.endsWith('.jsx')) {
        results.push(file);
      }
    }
  });
  return results;
}

// 检查依赖是否在代码中被使用
function checkDependencies() {
  const sourceFiles = readAllSourceFiles(path.join(__dirname, '..', 'src'));
  const sourceCode = sourceFiles.map(file => fs.readFileSync(file, 'utf-8')).join('\n');
  
  const dependencies = { ...packageJson.dependencies, ...packageJson.devDependencies };
  const unusedDeps = [];
  
  Object.keys(dependencies).forEach(dep => {
    // 特殊处理一些必要的依赖
    if (dep === 'electron' || dep === 'typescript' || dep === 'webpack') {
      return;
    }
    
    // 检查是否在源代码中被引用
    const importPatterns = [
      `from '${dep}'`,
      `from "${dep}"`,
      `require('${dep}')`,
      `require("${dep}")`,
      `import('${dep}')`
    ];
    
    const isUsed = importPatterns.some(pattern => sourceCode.includes(pattern));
    
    if (!isUsed) {
      unusedDeps.push(dep);
    }
  });
  
  console.log('可能未使用的依赖:');
  unusedDeps.forEach(dep => console.log(`- ${dep}`));
  
  if (unusedDeps.length === 0) {
    console.log('未发现明显未使用的依赖');
  }
}

checkDependencies();